﻿namespace Mp3Observer.Common.Language
{
    #region usings

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;

    #endregion

    /// <summary>
    /// this converters converts a DP of a DO with the language converter to the current culture.
    /// Its needed eg. for a TabItem, because the text comes from a datatemplate 
    /// </summary>
    /// <remarks>
    /// ConverterParameter empty : default case (textblock,,..)
    /// = "Button.Content": for Button bining to the accesstext of a button, but target is Button.Content.
    /// <example>
    /// <code>
    /// <![CDATA[ 
    /// <Button.Content>
    ///     <AccessText>
    ///         <MultiBinding Converter="{StaticResource LanguageMultiConverter}" Mode="OneWay" ConverterParameter="Button.Content">
    ///             <Binding RelativeSource="{RelativeSource Self}" Mode="OneWay" />
    ///             <Binding Path="CommandNameText" Mode="OneWay" />
    ///         </MultiBinding>
    ///     </AccessText>
    /// </Button.Content>
    /// ]]>
    /// </code>
    /// </example>
    /// = "Label.Content": for labels
    /// <example>
    /// <code>
    /// <![CDATA[ 
    /// <Label.Content>
    ///     <AccessText>
    ///         <MultiBinding Converter="{StaticResource LanguageMultiConverter}" Mode="OneWay" ConverterParameter="Label.Content">
    ///             <Binding RelativeSource="{RelativeSource Self}" Mode="OneWay" />
    ///             <Binding Path="Name" Mode="OneWay" />
    ///         </MultiBinding>
    ///     </AccessText>
    /// </Label.Content>
    /// ]]>
    /// </code>
    /// </example>
    /// </remarks>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Multi", Justification = "ok so")]
    public sealed class LanguageMultiConverter : IMultiValueConverter
    {
        #region IMultiValueConverter Members

        /// <summary>
        /// convert from 0:DO(target) the 1:resourcename to the language string with the language converter and the targetproperty "TextBlock.Text" as default.
        /// If 2:bool is set, and false, we do not translate anything. used for navigationitems ("alle.." is translated, and all other items are not, because they come from the database)
        /// If Property has a value, it creates a property for Button.Content or Button.ToolTip
        /// TODO: dynamic property creation.
        /// </summary>
        /// <param name="values">The array of values that the source bindings in the <see cref="T:System.Windows.Data.MultiBinding"/> produces. The value <see cref="F:System.Windows.DependencyProperty.UnsetValue"/> indicates that the source binding has no value to provide for conversion.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value.
        /// If the method returns null, the valid null value is used.
        /// A return value of <see cref="T:System.Windows.DependencyProperty"/>.<see cref="F:System.Windows.DependencyProperty.UnsetValue"/> indicates that the converter did not produce a value, and that the binding will use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue"/> if it is available, or else will use the default value.
        /// A return value of <see cref="T:System.Windows.Data.Binding"/>.<see cref="F:System.Windows.Data.Binding.DoNothing"/> indicates that the binding does not transfer the value or use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue"/> or the default value.
        /// </returns>
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values[1] == null || DependencyProperty.UnsetValue.Equals(values[1]))
                return String.Empty;

            string resName = (string)values[1];
            bool isTranslatable = values.Length == 2 ? true : DependencyProperty.UnsetValue.Equals(values[2]) ? true : ((bool)values[2]);

            if (!isTranslatable)
                return resName;

            LanguageConverter converter = new LanguageConverter(resName);
            DependencyProperty property = null;
            DependencyObject target = null;
            target = (DependencyObject)values[0];

            if (parameter != null)
            {
                if (parameter.ToString().Equals("Button.Content"))
                    property = Button.ContentProperty;
                if (parameter.ToString().Equals("Label.Content"))
                    property = Label.ContentProperty;
            }
            else
                property = TextBlock.TextProperty;

            // Do the Binding in the Culture
            LanguageContext.Instance.DoBinding(converter, target, property);
            return (string)target.GetValue(property);
        }

        /// <summary>
        /// Not Implemented
        /// </summary>
        /// <param name="value">The value that the binding target produces.</param>
        /// <param name="targetTypes">The array of types to convert to. The array length indicates the number and types of values that are suggested for the method to return.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// An array of values that have been converted from the target value back to the source values.
        /// </returns>
        /// <exception cref="NotImplementedException" />
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
